/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.bean.client;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "loginBean1")
@SessionScoped
public class LoginBean_c implements Serializable {
    private String errorMessage;
    Connection conn = null;
    public Boolean isLoggedIn = false;    
    private String username;
    private String password;

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Boolean getIsLoggedIn() {
        return isLoggedIn;
    }

    public void setIsLoggedIn(Boolean isLoggedIn) {
        this.isLoggedIn = isLoggedIn;
    }
    
    /**
     * Creates a new instance of LoginBean_c
     */
    public LoginBean_c() throws SQLException, ClassNotFoundException {
        conn= JDBCUtil.MyConn.getConnection();
    }
    
    public void loginForm() {

       try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT User_Name,User_Password,Account_Role FROM Accounts WHERE User_Name=? AND User_Password=? AND Account_Role='Admin'");
            pstmt.setString(1, this.username);
            pstmt.setString(2, this.password);
            ResultSet rs = pstmt.executeQuery();
            if(rs.next())
            {
                setIsLoggedIn(true);
                //return "home.xhtml?faces-redirect=true";
            }
            else {
                errorMessage = "Invalid Username or Password.";                
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
                  //  return "index.xhtml?faces-redirect=true";
    }
    
    public String logOut(){
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "index.xhtml?faces-redirect=true";
    }
    
//    public void toHome() throws IOException{
//        ExternalContext ec= FacesContext.getCurrentInstance().getExternalContext();
//        LoginBean_c loginBean = (LoginBean_c) ec.getSessionMap().get("loginBean");
//        if(loginBean ==null || !loginBean.getIsLoggedIn()){
//            ec.redirect(ec.getRequestContextPath()+"/index.xhtml");
//        }
//        //return "home.xhtml?faces-redirect=true";
//    }
}
